package com.greenant.store.activity;

import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.bean.StoreInfo;
import com.greenant.store.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class MyStoreActivity extends Activity {
	private View mViewBack;
	private View mViewSave;
	private EditText mEtAddress;
	private EditText mEtOutOfFreight;
	private EditText mEtFreight;
	private NumberPicker mOpenHourPicker;
	private NumberPicker mOpenMinutePicker;
	private NumberPicker mCloseHourPicker;
	private NumberPicker mCloseMinutePicker;
	
	private int mOpenHour;
	private int mOpenMinute;
	private int mCloseHour;
	private int mCloseMinute;
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mViewBack)) {
				finish();
			}
			else if(v.equals(mViewSave)) {
				if(AntHomeApplication.sCurrentStoreInfo != null) {
					StoreInfo storeInfo = AntHomeApplication.sCurrentStoreInfo;
					storeInfo.setAddress(mEtAddress.getText().toString());
					
					String openHourStr = (mOpenHour<10 ? "0"+mOpenHour : ""+mOpenHour);
					String openMinuterStr = (mOpenMinute<10 ? "0"+mOpenMinute : ""+mOpenMinute);
					String openStr = openHourStr + ":" + openMinuterStr;
					
					String closeHourStr = (mCloseHour<10 ? "0"+mCloseHour : ""+mCloseHour);
					String closeMinuteStr = (mCloseMinute<10 ? "0"+mCloseMinute : ""+mCloseMinute);
					String closeStr = closeHourStr + ":" + closeMinuteStr;
					storeInfo.setOpenTime(openStr);
					storeInfo.setCloseTime(closeStr);
					
					if(TextUtils.isEmpty(mEtFreight.getText())) {
						storeInfo.setDeliverycost(0);
					}
					else {
						storeInfo.setDeliverycost(Float.valueOf(mEtFreight.getText().toString()));
					}
					
					if(TextUtils.isEmpty(mEtOutOfFreight.getText())) {
						storeInfo.setStartcost(0);
					}
					else {
						storeInfo.setStartcost(Float.valueOf(mEtOutOfFreight.getText().toString()));
					}
					
					storeInfo.saveInBackground(new SaveCallback() {
						@Override
						public void done(AVException arg0) {
							if(arg0 == null) {
								UtilTools.showMyToast(MyStoreActivity.this, getResources().getString(R.string.my_store_save_successful));
							}
							else {
								UtilTools.showMyToast(MyStoreActivity.this, getResources().getString(R.string.my_store_save_failed));
								Log.d("CEXX", "CEXX => MyStoreActivity => save => arg0 : " + arg0);
							}
							
							finish();
						}
					});
				}
			}
		}
	};

	private Formatter mFormatter = new Formatter() {
        @Override
        public String format(int value) {
            String Str = String.valueOf(value);  
            if (value < 10) {  
                Str = "0" + Str;  
            }  
            return Str;  
        }
    };
	
	private OnValueChangeListener mOnValueChangeListener = new OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            if(picker.equals(mOpenHourPicker)) {
                Log.d("CEXX", "CEXX => onValueChange => mOpenHourPicker => oldVal : " + oldVal);
                Log.d("CEXX", "CEXX => onValueChange => mOpenHourPicker => newVal : " + newVal);
                mOpenHour = newVal;
            }
            else if(picker.equals(mOpenMinutePicker)) {
                Log.d("CEXX", "CEXX => onValueChange => mOpenMinutePicker => oldVal : " + oldVal);
                Log.d("CEXX", "CEXX => onValueChange => mOpenMinutePicker => newVal : " + newVal);
                mOpenMinute = newVal;
            }
            else if(picker.equals(mCloseHourPicker)) {
                Log.d("CEXX", "CEXX => onValueChange => mCloseHourPicker => oldVal : " + oldVal);
                Log.d("CEXX", "CEXX => onValueChange => mCloseHourPicker => newVal : " + newVal);
                mCloseHour = newVal;
            }
            else if(picker.equals(mCloseMinutePicker)) {
                Log.d("CEXX", "CEXX => onValueChange => mCloseMinutePicker => oldVal : " + oldVal);
                Log.d("CEXX", "CEXX => onValueChange => mCloseMinutePicker => newVal : " + newVal);
                mCloseMinute = newVal;
            }
        }
    };
    
    private OnScrollListener mOnScrollListener = new OnScrollListener() {
        @Override
        public void onScrollStateChange(NumberPicker view, int scrollState) {
        }
    };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.activity_my_store, getResources().getColor(R.color.green));
		
		mViewBack = findViewById(R.id.my_store_top_arrow_left);
		mViewSave = findViewById(R.id.my_store_top_arrow_right);
		mEtAddress = (EditText)findViewById(R.id.my_store_address_et);
		mEtOutOfFreight = (EditText)findViewById(R.id.my_store_out_of_freight_et);
		mEtFreight = (EditText)findViewById(R.id.my_store_freight_et);
		
		mOpenHourPicker = (NumberPicker)findViewById(R.id.hourpicker_open);	
		mOpenHourPicker.setOnValueChangedListener(mOnValueChangeListener);
		mOpenHourPicker.setOnScrollListener(mOnScrollListener);
		mOpenHourPicker.setMaxValue(23);
		mOpenHourPicker.setMinValue(0);
		mOpenHourPicker.setFormatter(mFormatter);
		
		mOpenMinutePicker = (NumberPicker)findViewById(R.id.minutepicker_open);	
		mOpenMinutePicker.setOnValueChangedListener(mOnValueChangeListener);
		mOpenMinutePicker.setOnScrollListener(mOnScrollListener);
		mOpenMinutePicker.setMaxValue(59);
		mOpenMinutePicker.setMinValue(0);
		mOpenMinutePicker.setFormatter(mFormatter);
		
		mCloseHourPicker = (NumberPicker)findViewById(R.id.hourpicker_close);	
		mCloseHourPicker.setOnValueChangedListener(mOnValueChangeListener);
		mCloseHourPicker.setOnScrollListener(mOnScrollListener);
		mCloseHourPicker.setMaxValue(23);
		mCloseHourPicker.setMinValue(0);
		mCloseHourPicker.setFormatter(mFormatter);
		
		mViewBack.setOnClickListener(mOnClickListener);
		mViewSave.setOnClickListener(mOnClickListener);
		
		mCloseMinutePicker = (NumberPicker)findViewById(R.id.minutepicker_close);
		mCloseMinutePicker.setOnValueChangedListener(mOnValueChangeListener);
		mCloseMinutePicker.setOnScrollListener(mOnScrollListener);
		mCloseMinutePicker.setMaxValue(59);
		mCloseMinutePicker.setMinValue(0);
		mCloseMinutePicker.setFormatter(mFormatter);
		
		init();
	}
	
	private void init() {
		if(AntHomeApplication.sCurrentStoreInfo == null) {
			return;
		}
		
		Log.d("CEXX", "CEXX => MyStoreActivity => getStoreid : " + AntHomeApplication.sCurrentStoreInfo.getStoreid());
		// 当前地址
		String address = AntHomeApplication.sCurrentStoreInfo.getAddress();
		Log.d("CEXX", "CEXX => MyStoreActivity => address : " + address);
		if(!TextUtils.isEmpty(address)) {
			mEtAddress.setText(address);
		}
		// 当前开门时间
		Date openDate = AntHomeApplication.sCurrentStoreInfo.getOpenTime();
		mOpenHour = openDate.getHours();
		mOpenMinute = openDate.getMinutes();
		mOpenHourPicker.setValue(mOpenHour);
		mOpenMinutePicker.setValue(mOpenMinute);
		// 当前关门时间
		Date closeDate = AntHomeApplication.sCurrentStoreInfo.getCloseTime();
		mCloseHour = closeDate.getHours();
		mCloseMinute = closeDate.getMinutes();
		mCloseHourPicker.setValue(mCloseHour);
		mCloseMinutePicker.setValue(mCloseMinute);
		// 当前免运费金额
		double startCose = AntHomeApplication.sCurrentStoreInfo.getStartcost();
		if(startCose < 0) {
			startCose = 0;
		}
		mEtOutOfFreight.setText("" + startCose);
		// 当前运费
		double deliveryCose = AntHomeApplication.sCurrentStoreInfo.getDeliverycost();
		if(deliveryCose < 0) {
			deliveryCose = 0;
		}
		mEtFreight.setText("" + deliveryCose);
	}
}
















